//
//  LoginViewController.m
//  RingLetter
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 FenglyNuo. All rights reserved.
//

#import "LoginViewController.h"
#import "Header.h"

@interface LoginViewController ()

@property (nonatomic, retain) UITextField *loginTextField;
@property (nonatomic, retain) UITextField *pWdTextField;

@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) UIButton *registerButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createSubView];
}



#pragma mark - SubView
- (void)createSubView {
    self.loginTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, kUIScreenWidth - 100, 50)];
    [self.view addSubview:_loginTextField];
    _loginTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _loginTextField.layer.borderWidth = 1;
    _loginTextField.placeholder = @"请输入账户";
    _loginTextField.clearButtonMode = UITextFieldViewModeAlways;
    
    self.pWdTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, kUIScreenWidth - 100, 50)];
    [self.view addSubview:_pWdTextField];
    _pWdTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _pWdTextField.layer.borderWidth = 1;
    _pWdTextField.placeholder = @"请输入密码";
    _pWdTextField.clearButtonMode = UITextFieldViewModeAlways;
    
    
    self.loginButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _loginButton.frame = CGRectMake(50, 300, 100, 50);
    [_loginButton setTitle:@"login" forState:(UIControlStateNormal)];
    [self.view addSubview:_loginButton];
    [_loginButton addTarget:self action:@selector(loginButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _registerButton.frame = CGRectMake(kUIScreenWidth - 150, 300, 100, 50);
    [_registerButton setTitle:@"register" forState:(UIControlStateNormal)];
    [self.view addSubview:_registerButton];
    [_registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
}



#pragma mark - 按钮点击方法
- (void)registerButtonAction:(UIButton *)button {
    
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.loginTextField.text password:self.pWdTextField.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
        
        NSLog(@"name = %@ pass = %@",username, password);
        
        //本地记录状态
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:LOGINSTATES];
        
        
        [[NSUserDefaults standardUserDefaults] setObject:username forKey:@"name"];
        [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"password"];
        
        //强制保存
        [[NSUserDefaults standardUserDefaults] synchronize];
        
    } onQueue:nil];
    
}

- (void)loginButtonAction:(UIButton *)button {
    
    NSString *userName = self.loginTextField.text;
    NSString *pwd = self.pWdTextField.text;
    
    if (!userName.length || !pwd.length) {
        NSLog(@"用户名或密码为空");
    } else {
        [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:userName password:pwd completion:^(NSDictionary *loginInfo, EMError *error) {
            
            if (!error) {
                NSLog(@"登录成功 %@", loginInfo);
                
                // 设置自动登录
                [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
                
                // 来主界面
                RosterViewController *cVC = [[RosterViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:cVC];
                nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通讯" image:nil tag:1000];
                
                MessageViewController *MVC = [[MessageViewController alloc] init];
                UINavigationController *MNav = [[UINavigationController alloc] initWithRootViewController:MVC];
                MVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"微信" image:nil tag:1001];
                
                UITabBarController *mainTabBar = [[UITabBarController alloc] init];
                mainTabBar.viewControllers = @[MNav, nav]; // tableBar数组，将创建的nav放里面就好
                [self.view.window setRootViewController:mainTabBar];
            }
            
        } onQueue:dispatch_get_main_queue()];
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
